import api from "../../../common/api.js";
import util from "../../../utils/util.js";
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgArray: '',
    name: '',
    tel: '',
    address: '',
    floor: '',
    wechatInfoState:false,//个人名片是否填写
    sessionInfo: '',
    setState:false,
    storeName:""
  },
  //企业认证
  goAuthentication(){
    wx.navigateTo({
      url: '/pages/mine/authentication/index'
    })
  },
  //选择图片
  chooseimage() {
    var that = this;
    util.updateImages(1,function(res){
      var tempFilePaths = res.tempFilePaths[0];
      util.uploadFiles(tempFilePaths, function (res){
        try {
          var id = JSON.parse(res.data)[0].id;
          api.settingEdit({ avatar: id}, function (res) {
            that.setData({
              imgArray: tempFilePaths
            })
          })
          
        } catch (error) {
          
        }
      })
    })
  },
  //跳转设置详情
  goSetDetail(e) {
    var title = e.currentTarget.dataset.title;
    
    if (title == "主营楼盘"){
      wx.navigateTo({
        url: '/pages/addHouse/searchHouse/searchHouse?mainBuilding=1',
      })
    }else{
      wx.navigateTo({
        url: '/pages/mine/setDetail/setDetail?title=' + title,
      })
    }
  },
  //跳转个人名片
  goSetWechatDetail() {
    wx.navigateTo({
      url: '/pages/mine/setWechatDetail/setWechatDetail?busiCard=' + this.data.busiCard,
    })
  },
  //获取设置页面数据
  getSessionInfo(){
    var that = this;
    
    api.getSessionInfo(null, function (res) {
      
      var data = res.data.content,floor = data.extProps.resblock,newFloor = [];
      floor.forEach((item,index)=>{
        if (item.hdResblockName && item.hdResblockName != ''){
          newFloor.push(item.hdResblockName)
        }else{
          newFloor.push(item.resblockName);
        }
      })
      var loginUserInfo = wx.getStorageSync("resblockName")
      if (loginUserInfo != '') {
        that.setData({
          floor: newFloor.join(" | "),
        })
        wx.setStorageSync("resblockName", '')
        return;
      }
      that.setData({
        imgArray: data.avatar,
        name: data.realName == null ? data.username : data.realName,
        tel: data.mobile,
        address: data.extProps.storeAdress,
        floor: newFloor.join(" | "),
        wechatInfoState: data.extProps.busiCard == null ? false: true,//个人名片是否填写
        busiCard: data.extProps.busiCard,
        sessionInfo: res.data.content,
        storeName:data.extProps.storeName,
      })
      var settingInfo = {
        name: data.realName == null ? data.username : data.realName,
        tel: data.mobile,
        address: data.extProps.storeAdress,
        floor: data.extProps.resblock,
        storeName:data.extProps.storeName,
      }
      wx.setStorageSync("settingInfo", settingInfo)
    })
  },

//失去焦点获取姓名
  winName:function(e){
    var that =this;
    that.data.name = e.detail.value;
  },
  //失去焦点获取手机号码
  winTel: function (e) {
    var that = this;
    that.data.tel = e.detail.value;
  },
  //失去焦点获取主营楼盘
  // winFloor: function (e) {
  //   var that = this;
  //   that.data.floor = e.detail.value;
  // },
  //失去焦点获取店铺名称
  winStoreName: function (e) {
    var that = this;
    that.data.storeName = e.detail.value;
  },
  //失去焦点获取店铺地址
  winAddress: function (e) {
    var that = this;
    that.data.address = e.detail.value;
  },
  
  saves: function () {
    setTimeout(this.save, 500)
  },

  save:function(){
    
    var that=this;
    var dataInfo = new Object();
    var dataInfos = new Object();
    var dataArrList = {
      storeName: that.data.storeName,//店铺名称
      storeAdress: that.data.address,//店铺地址
      resblock: that.data.floor,//主营楼盘
      mobile: that.data.tel,//手机号码
      realname: that.data.name,//姓名
    };
    wx.setStorageSync("settingInfo", dataArrList); 
    dataInfo.realname = that.data.name;
    dataInfo.mobile = that.data.tel;
    // dataInfo.resblock = that.data.floor;
    dataInfo.storeAdress = that.data.address;
    dataInfos.storeName = that.data.storeName;

   
    api.settingEdit(dataInfo, function (res) {
      
      wx.setStorageSync("settingInfo", dataInfo);
      wx.setStorageSync("setState", true);
      api.settingEdit(dataInfos, function (res) {
        if (res.statusCode != 200) {
          wx.showToast({
            title: '店址保存失败',
            icon: 'none',
            duration: 1000
          })
        }
      })
      if (res.statusCode == 200){
        // wx.showToast({
        //   title: '保存成功',
        //   icon: 'none',
        //   duration: 2500
        // })
            wx.navigateBack({
            delta: 1
          })
        // that.selectComponent("#setting").queryNumber();
        // var pages = getCurrentPages();
        // var prevPage = pages[pages.length - 2];  //上一个页面
        // prevPage.getAllnumber();
        app.globalData.indexPage.getAllnumber();
      }else{
        wx.showToast({
          title: '保存失败',
          icon: 'none',
          duration: 1000
        })
      }
     

    })
  },

   /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function() {
    var that = this;
      that.getSessionInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
      this.getSessionInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
  }
})
